package com.chuangjiangx.member.basic.ddd.application;

import com.chuangjiangx.commons.wx.WXSignUtils;
import com.chuangjiangx.commons.wx.jssdk.WXJssdkSign;
import com.chuangjiangx.member.basic.ddd.domain.service.MemberRedisDomainService;

import com.chuangjiangx.member.basic.ddd.dal.mapper.WXPublicUserInfoDalMapper;
import com.chuangjiangx.member.basic.ddd.dal.dto.AgentWXPublicUserInfo;
import com.chuangjiangx.member.basic.ddd.domain.model.msg.WXShare;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cj-ch
 * @date 2018/2/27 下午2:25
 */
@Service
public class ShareApplication {

    @Autowired
    private MemberRedisDomainService memberRedisDomainService;
    @Autowired
    private WXPublicUserInfoDalMapper wxPublicUserInfoDalMapper;

    /**
     * 微信分享
     * @param merchantId
     * @param url
     */
    public WXShare wxShare(Long merchantId, String url){
        WXShare wxShare = new WXShare();
        String ticket = memberRedisDomainService.getMerchantJsTicket(merchantId);
        if(StringUtils.isNotBlank(ticket)){
            WXJssdkSign wxJssdkSign = WXSignUtils.jssdkSign(ticket, url);
            wxShare.setWxJssdkSign(wxJssdkSign);
            AgentWXPublicUserInfo agentWXPublicUserInfo = wxPublicUserInfoDalMapper.selectByMerchantId(merchantId);
            if(agentWXPublicUserInfo != null){
                wxShare.setAppid(agentWXPublicUserInfo.getAuthorizerAppid());
            }
        }
        return wxShare;
    }
}
